﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.MyControl;

namespace ZK.Client
{
    public partial class FrmLoading : Form
    {
        public FrmLoading()
        {
            InitializeComponent();
            this.Resize += FrmLoading_Resize;
        }

        private static FrmLoading frmLoading;


        public delegate void mydelegate();
        public mydelegate eventMethod;

        public string[] Params;
        public delegate void mydelegateParam(params string[] strs);
        public mydelegateParam eventMethodParam;
        delegate void CloseFormCallback();

        private void FrmLoading_Resize(object sender, EventArgs e)
        {
            //SetWindowRegion.SetWindowSadius(20, this);
        }



        private void FrmLoading_Load(object sender, EventArgs e)
        {
             this.TransparencyKey = this.BackColor;
                Task.Factory.StartNew(() => {
                delegateEventMethod();
 
                CloseLoadingForm();
            });
        }




        /// <summary>
        /// 获取窗体
        /// </summary>
        /// <returns></returns>
        public static FrmLoading getLoading()
        {
            return frmLoading = new FrmLoading();

        }

        private void FrmLoading_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.IsDisposed)
            {
                this.Dispose(true);
            }
        }

        /// <summary>
        /// 线程执行方法
        /// </summary>
        private void delegateEventMethod()
        {
            try
            {
                if (this.eventMethod != null)
                {
                    eventMethod();
                }
                if (this.eventMethodParam != null)
                {
                    eventMethodParam(Params);
                }
            }
            catch (Exception ex)
            {
                MessageBoxShow(ex.Message);
            }
        }


        /// <summary>
        /// 委托关闭窗体
        /// </summary>
        public void CloseLoadingForm()
        {
            if (this.InvokeRequired)
            {
                CloseFormCallback d = new CloseFormCallback(CloseLoadingForm);
                this.Invoke(d, new object[] { });
            }
            else
            {

                this.Close();
                this.eventMethod = null;
                this.eventMethodParam = null;
            }
        }


        /// <summary>
        /// 绑定方法
        /// </summary>
        /// <param name="method"></param>
        public void SetExecuteMethod(mydelegate method)
        {
            this.eventMethod += method;
        }

        public void SetExecuteMethod(mydelegateParam method, params string[] strs)
        {
            this.eventMethodParam += method;
            Params = strs;
        }


        public static void ShowWaiting(mydelegate method )
        {
            FrmLoading.getLoading();
            frmLoading.SetExecuteMethod(method);
            //frmLoading.lblMes.LableShow("请稍后");
            frmLoading.ShowDialog();
        }


        public static void LabelShow(string mes)
        {
            frmLoading.lblMes.LableShow(mes);
            Thread.Sleep(2000);

        }

        public static void MessageBoxShow(string message)
        {
            MessageBox.Show(message, "提示", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);

        }
    }
}
